package com.auth.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.auth.mapper.SysRoleMapper;
import com.auth.mapper.SysUserMapper;
import com.auth.mapper.SysUserRoleMapper;
import com.common.model.po.SysRole;
import com.common.model.po.SysUserRole;
import com.common.model.po.UserDetails_Role;
import com.common.model.po.SysUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.auth.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Override
    public int addUser(SysUser user) {
        return sysUserMapper.insert(user);
    }

    @Override
    public SysUser findUserByName(String username) {
        return sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username",username));
    }

    @Override
    public List<SysUser> findAll() {
        return sysUserMapper.selectList(null);
    }

    @Override
    public SysUser register(SysUser user) {
        SysUser newMdsUser = new SysUser();
        BeanUtils.copyProperties(user, newMdsUser);
        //查询是否有相同用户名的用户
        List<SysUser> mdsUsers = sysUserMapper.selectList(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, newMdsUser.getUsername()));
        if (CollectionUtil.isNotEmpty(mdsUsers)) {
           return null;
        }
        //将密码进行加密操作
        String encodePassword = passwordEncoder.encode(user.getPassword());
        newMdsUser.setPassword(encodePassword);
        sysUserMapper.insert(newMdsUser);
        return newMdsUser;
    }

    @Override
    public UserDetails_Role loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserMapper.selectOne(new QueryWrapper<SysUser>().eq("username", username));
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        List<SysUserRole> userRoles = sysUserRoleMapper.selectList(new QueryWrapper<SysUserRole>().eq("sys_user_id", sysUser.getId()));
        if(userRoles.size() == 0 )return new UserDetails_Role(sysUser.getUsername(), sysUser.getPassword(), new ArrayList<SysRole>());
        List<String> roleIds = userRoles.stream().map(SysUserRole::getSysRoleId).collect(Collectors.toList());
        List<SysRole> sysRoles = sysRoleMapper.selectBatchIds(roleIds);
        return new UserDetails_Role(sysUser.getUsername(), sysUser.getPassword(), sysRoles);
    }
}
